package com.ByteDance.service.impl;

import com.ByteDance.model.po.FavoriteEntity;
import com.ByteDance.model.po.Relation;
import com.ByteDance.mapper.RelationMapper;
import com.ByteDance.model.po.UserInfos;
import com.ByteDance.model.vo.User;
import com.ByteDance.service.IRelationService;
import com.ByteDance.service.IUserService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * <p>
 * 用户关系表 服务实现类
 * </p>
 *
 * @author 朱哥
 * @since 2024-03-04
 */
@Service
@RequiredArgsConstructor
public class RelationServiceImpl extends ServiceImpl<RelationMapper, Relation> implements IRelationService {

    private final IUserService iUserService;
    @Override
    public Boolean action(int userId, int toUserId, int actionType) {
        Relation relation = lambdaQuery().eq(Relation::getFollowId, toUserId).eq(Relation::getFollowerId, userId).one();
        UserInfos userInfos = iUserService.getById(userId);
        UserInfos ToUserInfos = iUserService.getById(toUserId);

        if(actionType == 1){
            //关注操作--关注表增加一条记录
            //1.先看是否已经关注过
            if(relation != null){//关注过
                return false;
            }
            relation = new Relation();
            //2.构建relation
            relation.setFollowerId(userId);
            relation.setFollowId(toUserId);
            baseMapper.insert(relation);
            //关注完后，对应的toUserId的follower_count + 1和userId对应的follow_count + 1
            iUserService.lambdaUpdate()
                    .set(UserInfos::getFollowerCount,ToUserInfos.getFollowerCount() + 1)
                    .eq(UserInfos::getFollowerCount,ToUserInfos.getFollowerCount())
                    .eq(UserInfos::getUserId,toUserId)
                    .update();
            iUserService.lambdaUpdate()
                    .set(UserInfos::getFollowCount,userInfos.getFollowCount() + 1)
                    .eq(UserInfos::getFollowCount,userInfos.getFollowCount())
                    .eq(UserInfos::getUserId,userId)
                    .update();
            return true;
        }else if(actionType == 2){
            //1.先看是否已经关注过
            if(relation == null){//没有关注过
                return false;
            }
            //取消关注--关注表减少一条记录
            QueryWrapper<Relation> queryWrapper = new QueryWrapper<>();
            queryWrapper.eq("follow_id",toUserId).eq("follower_id",userId);
            baseMapper.delete(queryWrapper);
            //对应的toUserId的follower_count - 1
            iUserService.lambdaUpdate()
                    .set(UserInfos::getFollowerCount,ToUserInfos.getFollowerCount() - 1)
                    .eq(UserInfos::getFollowerCount,ToUserInfos.getFollowerCount())
                    .eq(UserInfos::getUserId,toUserId)
                    .update();
            iUserService.lambdaUpdate()
                    .set(UserInfos::getFollowCount,userInfos.getFollowCount() - 1)
                    .eq(UserInfos::getFollowCount,userInfos.getFollowCount())
                    .eq(UserInfos::getUserId,userId)
                    .update();
            return true;
        }
        //既不是1也不是2--不合法
        return false;
    }

    @Override
    public User[] getFollowList(int userId) {
        List<Relation> list = lambdaQuery().eq(Relation::getFollowerId, userId).list();
        User[] users = new User[list.size()];
        for (int i = 0; i < list.size(); i++) {
            Relation relation = list.get(i);
            Integer followId = relation.getFollowId();
            UserInfos userInfos = iUserService.getById(followId);//获取关注者id
            users[i] = new User();
            users[i].setUserId(followId);
            users[i].setUsername(userInfos.getUsername());
            users[i].setFollowCount(userInfos.getFollowCount());
            users[i].setFollowerCount(userInfos.getFollowerCount());
            //既然是关注列表，那肯定已经关注了
            users[i].setFollow(true);
            users[i].setAvatar(userInfos.getAvatar());
            users[i].setBackgroundImage(userInfos.getBackgroundImage());
            users[i].setSignature(userInfos.getSignature());
            users[i].setTotalFavorited(userInfos.getTotalFavorited());
            users[i].setWorkCount(userInfos.getWorkCount());
            users[i].setFavoriteCount(userInfos.getFavoriteCount());
        }
        return users;
    }

    @Override
    public User[] getFollowerList(int userId) {
        List<Relation> list = lambdaQuery().eq(Relation::getFollowId, userId).list();
        User[] users = new User[list.size()];
        for (int i = 0; i < list.size(); i++) {
            Relation relation = list.get(i);
            Integer followerId = relation.getFollowerId();//获取粉丝id
            UserInfos userInfos = iUserService.getById(followerId);
            users[i] = new User();
            users[i].setUserId(followerId);
            users[i].setUsername(userInfos.getUsername());
            users[i].setFollowCount(userInfos.getFollowCount());
            users[i].setFollowerCount(userInfos.getFollowerCount());
            Relation relation1 = lambdaQuery().eq(Relation::getFollowId, followerId).eq(Relation::getFollowerId, userId).one();
            if(relation1 == null){
                users[i].setFollow(false);
            }else {
                users[i].setFollow(true);
            }
            users[i].setAvatar(userInfos.getAvatar());
            users[i].setBackgroundImage(userInfos.getBackgroundImage());
            users[i].setSignature(userInfos.getSignature());
            users[i].setTotalFavorited(userInfos.getTotalFavorited());
            users[i].setWorkCount(userInfos.getWorkCount());
            users[i].setFavoriteCount(userInfos.getFavoriteCount());
        }
        return users;
    }

    @Override
    public User[] getFriendList(int userId) {
        List<Relation> list = lambdaQuery().eq(Relation::getFollowerId, userId).list();
        User[] users = new User[list.size()];
        for (int i = 0; i < list.size(); i++) {
            Relation relation = list.get(i);
            Integer followId = relation.getFollowId();// 关注的人的id
            //看关注id是否也是你的粉丝
            Relation relation1 = lambdaQuery().eq(Relation::getFollowId, userId).eq(Relation::getFollowerId, followId).one();
            if(relation1 != null){//你关注的人也关注了你--互关
                UserInfos userInfos = iUserService.getById(followId);
                users[i] = new User();
                users[i].setUserId(followId);
                users[i].setUsername(userInfos.getUsername());
                users[i].setFollowCount(userInfos.getFollowCount());
                users[i].setFollowerCount(userInfos.getFollowerCount());
                //既然是好友，那肯定已经关注了
                users[i].setFollow(true);
                users[i].setAvatar(userInfos.getAvatar());
                users[i].setBackgroundImage(userInfos.getBackgroundImage());
                users[i].setSignature(userInfos.getSignature());
                users[i].setTotalFavorited(userInfos.getTotalFavorited());
                users[i].setWorkCount(userInfos.getWorkCount());
                users[i].setFavoriteCount(userInfos.getFavoriteCount());
            }
        }
        return users;
    }
}
